package com.bitejiuyeke.forum.controller;

import com.bitejiuyeke.forum.common.AppResult;
import com.bitejiuyeke.forum.common.ResultCode;
import com.bitejiuyeke.forum.exception.ApplicationException;
import com.bitejiuyeke.forum.model.Board;
import com.bitejiuyeke.forum.services.IBoradService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 666
 * Date: 2023-12-04
 * Time: 22:20
 * @author 666
 */
@Slf4j
@RestController
@Api(tags = "板块接口")
@RequestMapping("/board")

public class BoradController {
    /**
     从配置⽂件中获取主⻚中显⽰的版块个数，默认为9
     */
    @Value("${bit-forum.index.board-num:9}")
    private Integer indexBoardNum;
    @Resource
    private IBoradService iBoradService;
    /**
     * 查询首页板块列表
     */
    @ApiOperation("获取首页板块列表")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList() {
      log.info("首页板块为"+ indexBoardNum);
        List<Board> boards = iBoradService.selectByNum(indexBoardNum);
        if(boards == null){
            boards = new ArrayList<>();
        }
        return AppResult.success(boards);
    }
    @ApiOperation("获取板块信息")
    @GetMapping("/getById")
    public  AppResult<Board> getById(@ApiParam("板块Id") @RequestParam("id") @NonNull Long id){
        Board board = iBoradService.selectById(id);
        // 判断是否为空
        if (board == null || board.getDeleteState()==1){
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        return AppResult.success(board);
    }
}
